/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.serv.recharge.service;

import com.synjones.core.util.StringUtil;
import com.synjones.pay.bank.api.IBankJump;
import com.synjones.pay.bank.wisedu.service.WisBankJumpImpl;
import com.synjones.pay.model.common.CommonConstant;
import com.synjones.pay.model.context.IPayContext;
import com.synjones.pay.model.context.StatusInfo;
import com.synjones.pay.model.recharge.RechargeReqBean;
import com.synjones.pay.model.recharge.RechargeRespBean;
import com.synjones.pay.serv.dao.bean.RechargeDao;
import com.synjones.pay.serv.dao.repository.RechargeDaoRepository;
import com.synjones.pay.serv.recharge.helper.RechargeHelper;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 *
 * @author hsd
 */
@Service("rechargeService")
public class RechargePayServiceImpl implements IRechargeService {

    private final static Logger logger = LoggerFactory.getLogger(RechargePayServiceImpl.class);

    @Resource
    private RechargeDaoRepository rechargeDao;

    @Override
    public RechargeRespBean tradeProcess(RechargeReqBean reqBean) {
        //1.组装dao
        RechargeDao dao = RechargeHelper.assemDaoData(reqBean);
        //2.保存到数据库
        if (this.save(dao)) {
            return getTradeResult(dao);
        } else {
            return RechargeHelper.failRespData(reqBean);
        }

    }

    @Override
    public boolean save(RechargeDao dao) {
        try {
            rechargeDao.save(dao);
            return true;
        } catch (Exception e) {
            logger.error("保存订单出错：", e);
            return false;
        }
    }

    @Override
    public RechargeRespBean getTradeResult(RechargeDao dao) {
        //1.组装ctx
        IPayContext ctx = RechargeHelper.assemTradeData(dao);
        //2.获取组装要提交给网银的参数
        IBankJump jump = new WisBankJumpImpl();
        Map<String, String> reqMap = jump.doPay(ctx);
        //3.返回组装的respBean给业务平台
        RechargeRespBean respBean = new RechargeRespBean();
        BeanUtils.copyProperties(dao, respBean);
        String serialCode = reqMap.get(CommonConstant.JUMPL_URL);
        if (!StringUtil.isNullOrEmpty(serialCode)) {
            //4.金智返回的的支付码作为流水号支付，因为金智的支付码就是流水号订单的对账就是这个支付码
            if (!updateSerialNo(serialCode, dao.getOrderId())) {
                respBean.setStatusCode(StatusInfo._9000.code());
                respBean.setStatusDesc("保存支付码失败");
            } else {
                respBean.setStatusCode(StatusInfo._0000.code());
                respBean.setStatusDesc(StatusInfo._0000.desc());
                respBean.setSerialNo(serialCode);
            }
        } else {
            respBean.setStatusCode(StatusInfo._1002.code());
            respBean.setStatusDesc(StatusInfo._1002.desc());
        }
        respBean.setRespMap(reqMap);
        return respBean;
    }

    @Override
    public boolean updateSerialNo(String serialNo, String orderId) {
         try {
            return rechargeDao.updateSerialNo(serialNo, orderId) >= 1;
        } catch (Exception e) {
            logger.error("更新订单{} 支付码出错：", orderId, e);
            return false;
        }
    }

}
